package device;

import java.util.ArrayList;
import java.util.Iterator;

public class Router {
	public Router() {
		routerID = ++routerNum;
	}

	public Router(String routerAddr) {
		routerID = ++routerNum;
		this.routerAddr = routerAddr;
	}

	// public void addDirectNeighbor(Host neighbor) {
	// directNeighbors.add(neighbor);
	// }
	//
	// public Router getDirectNeighbor() {
	// iDirectNeighbor = directNeighbors.iterator();
	// return getNextDirectNeighbor();
	// }
	//
	// public Router getNextDirectNeighbor() {
	// if (iDirectNeighbor.hasNext())
	// return (Router) iDirectNeighbor.next();
	// else
	// return null;
	// }

	public int getRouterID() {
		return routerID;
	}

	public boolean isVisited() {
		return visited;
	}

	public void setVisited(boolean visited) {
		this.visited = visited;
	}
	
	public void insertHost(Host h) {
		for (Iterator<SubNet> itr = subNets.iterator(); itr.hasNext(); ) {
			SubNet s = itr.next();
			if (s.hosts.contains(h))
				break; // the host has been existed yet
			if (s.in(h)) {
				s.hosts.add(h);
				break;
			}
		}
	}

	@Override
	public boolean equals(Object o) {
		if (o instanceof Router) {
			Router r = (Router) o;
			if (r.getRouterID() == routerID || r.routerName.equals(routerName)
					|| r.routerAddr.equals(routerAddr))
				return true;
		}
		return false;
	}

	// public LinkedList nextHop = new LinkedList();
	// public LinkedList directNeighbors = new LinkedList();
	// private Iterator iDirectNeighbor = directNeighbors.iterator();

	/** All the subnets that are connected to this router */
	public ArrayList<SubNet> subNets = new ArrayList<SubNet>();
	private int routerID;
	public String routerName;
	public String routerAddr;
	private boolean visited = false;
	private static int routerNum = 0;
}
